package com.taru.wxhsh.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.Detail;
import com.taru.wxhsh.pojo.Order;
import com.taru.wxhsh.pojo.Product;
import com.taru.wxhsh.pojo.User;
import com.taru.wxhsh.service.OrderService;
import com.taru.wxhsh.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@CrossOrigin
public class OrderApi {


    Logger logger  = LoggerFactory.getLogger(OrderApi.class);

    @Autowired
    OrderService orderService;
    /**
     * 查询历史订单
     * @return
     */
    @GetMapping("/api/order/selectHistory")
    public Object selectHistory(@RequestParam("userId") String userId)
    {
        JsonResult<List<Order>> result=null;
        try{

           List<Order> orders =orderService.selcetById(userId);
           result =new JsonResult<>(JResponseConstants.STATUS_OK,"查询历史订单成功",orders);

        }catch (Exception ex){
            logger.error("查询历史订单异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询历史订单异常");
        }
        return  result;

    }

    /**
     * 下订单
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/order/commit")
    public Object commit(Order order, @RequestParam("orderUserId")   String userId){
        JsonResult<String> result = null;
        try {
            order.setUser(new User(userId));
            String orderId =orderService.addOrder(order);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"提交订单成功",orderId);
        }catch (Exception ex){
            logger.error("提交订单异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"提交订单异常");
        }
        return  result;
    }



    /**
     * 根据订单id查询订单详情和关联商品
     * @param orderid
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/order/detail")
    public Object qryDetail( @RequestParam("orderId")  String orderid){
        JsonResult< List<Detail>>  result = null;
        try {
            List<Detail> list = orderService.selectByOrderId(orderid);
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_OK,"查询订单详情成功",list);
        }catch (Exception ex){
            logger.error("查询订单详情异常:{}",ex.getMessage());
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_ERROR,"查询订单详情异常");
        }
        return result;
    }

}
